﻿#include "taiji.h"
#include <QtWidgets/QApplication>
#include <QFile>
#include <QTextCodec>
#include <QDebug>
#include <QMessageBox>
#include <QDirIterator>
#include <QDir>
#include <QLibrary>
#include "interface.h"
#include "componentlist.h"
void ScanTaiJiPlugins(QString pluginsdir)
{
    qDebug() << pluginsdir;
    QDirIterator it(pluginsdir,QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot);	//遍历所有目录和文件
    while (it.hasNext())//存在
    {
        QString name = it.next();//读取
        QFileInfo info(name);
        if (info.isDir())//判断是目录
        {
            //将目录加入运行环境变量
            continue;
        }
        else
        {
            if (info.suffix() == "dll")
            {
                qDebug() << pluginsdir+ "/" +info.fileName();
                qputenv("PATH", QString("%1;%2").arg(qgetenv("PATH"), pluginsdir+ "/" +info.fileName().remove(".dll")).toLocal8Bit());
                QLibrary hdll(pluginsdir+ "/" +info.fileName().toLocal8Bit().data());
                if(hdll.load()){
                    GetComponentNameFunc namefunc = (GetComponentNameFunc)hdll.resolve("GetComponentName");
                    GetNewComponentFunc func = (GetNewComponentFunc)hdll.resolve("GetNewComponent");
                    GetComponentDescriptionFunc desfunc = (GetComponentDescriptionFunc)hdll.resolve("GetComponentDescription");
                    //qDebug() << namefunc << func;
                    if(namefunc&&func){
                        qDebug() << namefunc();
                        g_FuncMap[namefunc()] = pluginsdir+ "/" +info.fileName().toLocal8Bit().data();
                        g_Des = g_Des + namefunc() + "@@@@" + desfunc() + "####";
                    }
                }else{
                    qDebug() << pluginsdir+ "/" +info.fileName().toLocal8Bit().data() << "load failed";
                    qDebug() << hdll.errorString();
                }
            }
        }
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //扫描插件
#ifdef _WIN64
    if (argc < 2) {
        return 0;
    }
    qDebug() << "PLUGIN_CPU_x86_64:" << argv[1] ;
    QString x86Des;
    ScanTaiJiPlugins(QCoreApplication::applicationDirPath().replace("Runtimes","TaiJiPlugins"));
    if(argv[1]==QString("--help")){
        if (argc < 3) {
            return 0;
        }
        x86Des = QString::fromLocal8Bit(argv[2]);
        CompenentList list;
        list.AddDes(x86Des);
        list.AddDes(g_Des);
        list.show();
        return a.exec();
    }
#else
    qDebug() << "PLUGIN_CPU_X86:";
    ScanTaiJiPlugins(QCoreApplication::applicationDirPath().replace("Runtimes","TaiJiPlugins"));
#endif

    TaiJi tj;
    if (argc < 2) {
        tj.Excute(QStringLiteral("帮助"), TJ_SCRIPT);
        return 0;
    }else{
        QString arg1 = QString::fromLocal8Bit(argv[1]);
        if (arg1.endsWith(".tj")) {
            QFile file(arg1);
            file.open(QFile::ReadOnly | QFile::Text);
            QTextCodec *codec = QTextCodec::codecForName("UTF8");
            QString content = codec->toUnicode(file.readAll());

            file.close();
            tj.Excute(content, TJ_SCRIPT);
        }else if(arg1 == "--help"){
            tj.Excute(QStringLiteral("帮助"), TJ_SCRIPT);
        }else if(arg1 == "-call"){
            if(argc < 3){
                return 0;
            }
            tj.Excute(QString::fromLocal8Bit(argv[2]), TJ_SCRIPT);
        }
    }
    //QMessageBox::about(NULL,"",content);
    return 0;
}
